/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.fileloader.util;

import edu.enac.fileloader.exception.FileLoaderException;
import edu.enac.geoapp.model.Format;
import edu.enac.geoapp.model.Product;

/**
 * Class that can parse a filename in order to identify
 * the meteo product type and the file format.
 * @author mathieu
 *
 */
public class FileNameParser {
	private String filename;
	private String dir;
	private Format format;
	private Product product;

	/**
	 * Parse the given filename
	 * @param s the filename
	 * @throws FileLoaderException If the name cannot be parsed
	 */
	public void parse(String s) throws FileLoaderException {
		try {
			dir = s.substring(s.indexOf("dir:") + 4, s.indexOf(" name"));
			filename = s
					.substring(s.indexOf("name:") + 5, s.indexOf(" format"));
			String suffix = s.substring(s.indexOf("format:") + 7, s
					.indexOf(" product:"));
			format = Format.getFormat(suffix);
			String productName = s.substring(s.indexOf("product:") + 8);
			product = Product.getProduct(productName);
		} catch (StringIndexOutOfBoundsException e) {
			throw new FileLoaderException("Unable to parse file information");
		}
		if (format == null || product == null) {
			throw new FileLoaderException("Unable to parse file information");
		}
	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @return the format
	 */
	public Format getFormat() {
		return format;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}


	/**
	 * @return the directory where the file must be downloaded
	 */
	public String getDir() {
		return dir;
	}

}
